package com.rome.hotel.data.service.impl;

import java.util.List;
import com.rome.hotel.common.utils.DateUtils;
import com.rome.hotel.data.domain.HotelRoom;
import com.rome.hotel.data.domain.HotelUser;
import com.rome.hotel.data.mapper.HotelRoomMapper;
import com.rome.hotel.data.utils.RegexUtils;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rome.hotel.data.mapper.HotelFloorMapper;
import com.rome.hotel.data.domain.HotelFloor;
import com.rome.hotel.data.service.IHotelFloorService;

/**
 * 楼层Service业务层处理
 * 
 * @author rome
 * @date 2023-12-20
 */
@Service
public class HotelFloorServiceImpl implements IHotelFloorService 
{
    @Autowired
    private HotelFloorMapper hotelFloorMapper;

    @Autowired
    private HotelRoomMapper hotelRoomMapper;
    /**
     * 查询楼层
     * 
     * @param floorId 楼层主键
     * @return 楼层
     */
    @Override
    public HotelFloor selectHotelFloorByFloorId(Long floorId)
    {
        return hotelFloorMapper.selectHotelFloorByFloorId(floorId);
    }

    /**
     * 查询楼层列表
     * 
     * @param hotelFloor 楼层
     * @return 楼层
     */
    @Override
    public List<HotelFloor> selectHotelFloorList(HotelFloor hotelFloor)
    {
        return hotelFloorMapper.selectHotelFloorList(hotelFloor);
    }

    /**
     * 新增楼层
     * 
     * @param hotelFloor 楼层
     * @return 结果
     */
    @Override
    public int insertHotelFloor(HotelFloor hotelFloor)
    {
//        防御性编程
        Assert.notNull(hotelFloor,"参数位空");
        Assert.state(RegexUtils.testNumber(hotelFloor.getFloorNumber().toString()),"参数只能是数字");
        Assert.notNull(hotelFloor.getFloorName(),"楼层名称不能为空");
        Assert.state(RegexUtils.testFloorName(hotelFloor.getFloorName()),"只能是汉字、数字、字母的组合不能包含其他字符");
        hotelFloor.setCreateTime(DateUtils.getNowDate());
        HotelFloor hotelFloorQuery = hotelFloorMapper.selectHotelFloorByFloorId(hotelFloor.getFloorId());
        Assert.notNull(hotelFloorQuery!=null,"参数异常");
        Assert.state(hotelFloorMapper.selectHotelFloorByFloorNumber(hotelFloor.getFloorNumber())==null,"楼层号不能相同");
        return hotelFloorMapper.insertHotelFloor(hotelFloor);
    }

    /**
     * 修改楼层
     * 
     * @param hotelFloor 楼层
     * @return 结果
     */
    @Override
    public int updateHotelFloor(HotelFloor hotelFloor)
    {
        //        防御性编程
        Assert.notNull(hotelFloor,"参数位空");
        Assert.state(RegexUtils.testNumber(hotelFloor.getFloorNumber().toString()),"参数只能是数字");
        Assert.notNull(hotelFloor.getFloorName(),"楼层名称不能为空");
        Assert.state(RegexUtils.testFloorName(hotelFloor.getFloorName()),"只能是汉字、数字、字母的组合不能包含其他字符");
        HotelFloor hotelFloorQuery = hotelFloorMapper.selectHotelFloorByFloorId(hotelFloor.getFloorId());
        Assert.notNull(hotelFloorQuery,"参数异常");
        if(hotelFloor.getFloorNumber() != hotelFloorQuery.getFloorNumber()){
            HotelRoom queryHotelRoom = new HotelRoom();
            queryHotelRoom.setFloorId(hotelFloorQuery.getFloorNumber());
            List<HotelRoom> hotelRooms = hotelRoomMapper.selectHotelRoomList(queryHotelRoom);
            for (HotelRoom hotelRoom:hotelRooms
                 ) {
                hotelRoom.setFloorId(hotelFloor.getFloorNumber());
                System.out.println(hotelRoom);
                hotelRoomMapper.updateHotelRoom(hotelRoom);
            }
        }
        return hotelFloorMapper.updateHotelFloor(hotelFloor);
    }

    /**
     * 批量删除楼层
     * 
     * @param floorIds 需要删除的楼层主键
     * @return 结果
     */
    @Override
    public int deleteHotelFloorByFloorIds(Long[] floorIds)
    {
        for (Long id:floorIds
        ) {

            Assert.notNull(hotelFloorMapper.selectHotelFloorByFloorId(id),"参数异常");
        }
        return hotelFloorMapper.deleteHotelFloorByFloorIds(floorIds);
    }

    /**
     * 删除楼层信息
     * 
     * @param floorId 楼层主键
     * @return 结果
     */
    @Override
    public int deleteHotelFloorByFloorId(Long floorId)
    {

        //        1.防御性编程
        HotelFloor hotelFloor = hotelFloorMapper.selectHotelFloorByFloorId(floorId);
        Assert.notNull(hotelFloor,"参数异常");
        return hotelFloorMapper.deleteHotelFloorByFloorId(floorId);
    }
}
